package api.model;

import m.common.model.FieldMeta;
import m.common.model.LinkTableMeta;
import m.common.model.TableMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.system.cache.FlushCache;
import manage.model.AdminLogin;
import manage.model.OrgGroupView;
import manage.model.StatusModel;

import java.util.List;

@TableMeta(name="ot_api_authinfo",description="鉴权信息")
public class ApiAuthinfo extends StatusModel implements FlushCache {

    @FieldMeta(name="name",type= FieldType.STRING,length=100,description="名称")
    private String name;
    @FieldMeta(name="description",type=FieldType.STRING,length=1000,description="描述")
    private String description;
    @LinkTableMeta(name="admin_oid",table= AdminLogin.class,description="登录账号")
    private AdminLogin admin;
    @LinkTableMeta(name="org_oid",table= OrgGroupView.class,description="登录机构")
    private OrgGroupView org;
    @FieldMeta(name="auth_id",type= FieldType.STRING,length=20,description="鉴权id")
    private String authId;
    @FieldMeta(name="auth_key",type= FieldType.STRING,length=100,description="鉴权key")
    private String authKey;
    @FieldMeta(name="auth_time",type=FieldType.STRING,length=10,description="鉴权误差",dictOption = "0:不限制,10:10秒,300:5分钟,3600:1小时")
    private String authTime;
    @FieldMeta(name="type",type=FieldType.STRING,length=10,defaultValue = "B",description="类型",dictOption = "A:登录,B:接口")
    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public AdminLogin getAdmin() {
        return admin;
    }

    public void setAdmin(AdminLogin admin) {
        this.admin = admin;
    }

    public OrgGroupView getOrg() {
        return org;
    }

    public void setOrg(OrgGroupView org) {
        this.org = org;
    }

    public String getAuthId() {
        return authId;
    }

    public void setAuthId(String authId) {
        this.authId = authId;
    }

    public String getAuthKey() {
        return authKey;
    }

    public void setAuthKey(String authKey) {
        this.authKey = authKey;
    }

    public String getAuthTime() {
        return authTime;
    }

    public void setAuthTime(String authTime) {
        this.authTime = authTime;
    }

    @Override
    public ApiAuthinfo getCacheModel(String authId) throws Exception {
        List<ApiAuthinfo> list= ModelQueryList.getModelList(ApiAuthinfo.class,new String[]{"*","admin.*","org.*"},null,
                QueryCondition.eq("authId",authId));
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }
}
